home *** CD-ROM | disk | FTP | other *** search
- From: SJSELS@msn.com (Leigh Saunders)
- Subject: RE: integer-2-string
- Date: 19 Jan 96 05:09:59 -0800
- References: <4dht24$g0g@ratree.psu.ac.th>
- Message-ID: <00001a80+00006eb6@msn.com>
- Path: news.msn.com!msn.com
- Newsgroups: comp.lang.c
- Organization: The Microsoft Network (msn.com)
-
- >,Hi there,
-
- > In C on UNIX,how can I convert integer to string ??
- >Function itoa() doesn't work.......
-
- Well if itoa() doesn't do it for you I would use something like this:
-
- char *int_to_ascii(int *value)
- {
- char *temp=NULL;
-
- temp=malloc(6);
- sprintf(temp,"%-5d",value);
- return(temp);
- }
- The calling function will have to be responsible for free()'ing the
- returned string. Some small changes can be done to make a more
- generic and portable function by using a char[] of a max length of a
- #define that coresponds with the max digits of the max int on the
- system it is compiled on and using that to store the string and then
- get it's size with strlen() and malloc the char *temp to pass out of
- the function. The above example is very crude and would only work if
- you knew that 5 digits was the max digits that value would ever
- contain.
-